<template>
	<view class="comment-create">
		<u--form labelPosition="left" :model="formData" :rules="rules" ref="uForm">

			<view class="warpper">
				<u-form-item labelWidth="0" prop="content">
					<u--textarea v-model="formData.content" placeholder="请输入内容" :height="200" count maxlength="200"></u--textarea>
				</u-form-item>
				<u-form-item labelWidth="0" prop="visuals">
					<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10"
						:width="100" :height="100"></u-upload>
				</u-form-item>
			</view>
			<view class="warpper">
				<u-form-item label="研学评价" labelWidth="90" prop="grade">
					<view class="star-warpper">
						<view class="star-text">{{starText}}</view>
						<u-rate v-model="formData.grade" :size="24"></u-rate>
					</view>
				</u-form-item>
			</view>
		</u--form>
		<view class="footer-btn" style="background-color: #FA3534;" @click="submit">
			发布
		</view>
	</view>
</template>

<script>
	import {
		apiURL,
		StarLevel
	} from '@/utils/contant.js';
	export default {
		data() {
			return {
				formData: {
					orderId: '',
					grade: 5,
					content: '',
					visuals: [],
				},
				rules: {
					'content': {
						type: 'string',
						required: true,
						message: '请输入内容',
						trigger: ['blur']
					},
				},
				starText: '非常好',
				fileList: [],
			}
		},
		onLoad(options) {
			this.formData.orderId = options.id;
		},
		// onReady() {
		// 	//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		// 	this.$refs.uForm.setRules(this.rules)
		// },
		watch: {
			'formData.count'(val) {
				this.starText = StarLevel.find((item) => item.value === val).name;
			},
		},
		methods: {
			async getInfo(id) {
				const res = await this.$api('/study/mobile/personal/center/info?id=' + id, 'post', {
					id
				});
				this.form = res.data;
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this.fileList.length;
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this.fileList[fileListLen];
					this.fileList.splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result.src,
							id: result.id
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: apiURL + '/sys/oss/upload',
						filePath: url,
						name: "file",
						header: {
							token: uni.getStorageSync('token'),
						},
						success: (uploadFileRes) => {
							console.log(uploadFileRes);
							const data = JSON.parse(uploadFileRes.data);
							if (data.code === 0) {
								resolve(data.data);
							}
						},
					});
				});
			},
			submit() {
				this.formData.visuals = this.fileList.map((item) => item.id);
				this.$refs.uForm.validate().then(async () => {
					uni.showLoading({
						title: '正在保存...',
					});
					const res = await this.$api('/study/mobile/comment/comment', 'post', this.formData);
					uni.hideLoading();
					if (res.code === 0) {
						uni.navigateBack();
					}
				})

			},
		},
	}
</script>

<style lang="scss" scoped>
	.comment-create {
		border-radius: 20rpx;
	}

	.star-warpper {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		padding: 20rpx 0;

		.star-text {
			padding-right: 20rpx;
			color: #666;
		}
	}

	.warpper {
		background-color: #fff;
		padding: 0 20rpx;
		margin: 20rpx;
		border-radius: 20rpx;
	}
</style>